/**
 * 
 */
package com.angis.fx.activity.search;

import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;

import com.angis.fx.activity.R;
import com.angis.fx.activity.service.CheckInfoService;
import com.angis.fx.activity.service.FieldSearchService;
import com.angis.fx.data.ChangsuoInformation;
import com.angis.fx.util.DataParseUtil;

/**
 * @author Han
 *
 */
public class CheckInfoActivity extends Activity {
	
	private ChangsuoInformation mChangsuoInfo;
	
	private ListView mListView;
	
	private List<Map<String, String>> mValueMapList;
	
	private UpdateReceiver receiver;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.checkinfodetails);
		mChangsuoInfo = (ChangsuoInformation)getIntent().getExtras().get("selectedcs");
		initActivity();
		
		receiver = new UpdateReceiver();
        IntentFilter filter = new IntentFilter();
         // find a action. com.hoperun.gpsService is defined in GpsService class
        filter.addAction("checkinfo");
        this.registerReceiver(receiver, filter);
        Intent intent = new Intent(CheckInfoActivity.this,CheckInfoService.class);  
        intent.putExtra("csid", mChangsuoInfo.getCsId());
        startService(intent); 
		
//		mValueMapList = new 
		
	}

	private void initActivity() {
		mListView = (ListView) findViewById(R.id.checkinfolist);
	}
	
	public class UpdateReceiver extends BroadcastReceiver{
	    
        @Override
        public void onReceive(Context context, Intent intent) {
        	CheckInfoActivity.this.unregisterReceiver(receiver);
        	String response = intent.getStringExtra("results");
        	try{
	        	if (response.equals("")){
	        		Toast.makeText(CheckInfoActivity.this, "没有巡查信息!", Toast.LENGTH_LONG).show();
	        		return;
	        	} else {
	            	if (response.indexOf("^") != -1) {
	            		mValueMapList = DataParseUtil.parseCheckInfo(DataParseUtil.handleResponse(response));
	            		SimpleAdapter lAdapter = new SimpleAdapter(CheckInfoActivity.this, mValueMapList,
	            				R.layout.checkinfodetailslayout, new String[] { "", "time",
	            						"status", "result", "person" }, new int[] { 0,
	            						R.id.checktime, R.id.checkstatus,
	            						R.id.checkresult, R.id.checkperson });
	            		mListView.setAdapter(lAdapter);
	        		}else {
	        			Toast.makeText(CheckInfoActivity.this, "没有巡查信息!", Toast.LENGTH_LONG).show();
	            		return;
	        		}
	        	}
        	}catch (Exception e){
        		Toast.makeText(CheckInfoActivity.this, "解析数据出错!", Toast.LENGTH_LONG).show();
        	}
        }
        
	}
}
